/*************************************************************************
	> File Name: 003.异或运算交换两个数值.c
	> Author: Maureen 
	> Mail: Maureen@qq.com 
	> Created Time: 五  8/20 19:53:35 2021
 ************************************************************************/

#include <stdio.h>

int main() {
    int a = 7, b = 3;
    //交换数值的一般做法：
    /*int temp = a;
    a = b;
    b = temp;*/

    //异或运算交换两个数值
    a ^= b;
    b ^= a;
    a ^= b;

    printf("swap: a = %d, b = %d\n", a, b);

    return 0;
}
//分析：利用了异或运算相同为0，不同为1的特点。上面三步分解一下就是：
//a ^= b; ==> a = a ^ b;
//b ^= a; ==> b = b ^ (a ^ b) = 0 ^ a = a;
//a ^= b; ==> a = (a ^ b) ^ (a) = 0 ^ b = b;
